import { Node, UITransform, Layers, SpriteFrame } from 'cc'
export const createUINode = (name: string = '') => {
  const node = new Node(name)
  const transform = node.addComponent(UITransform)
  transform.setAnchorPoint(0, 1)
  // 设置节点所在图层， cocos这里的图从用的是 二进制码，比如 UI_2D图层的二进制码是 10000000000000000000000000
  // 这里就是将 0 位置上的1 向左移动 25位
  node.layer = 1 << Layers.nameToLayer('UI_2D')
  return node
}
// 在给定的范围里进行随机
export const randomByRange = (start: number, end: number) => {
  return Math.floor(start + Math.random() * (end - start))
}

// 获取字符串中的括号里的数字
export const getNumberInString = (str: string) => {
  const regex = /\((\d+)\)/g
  let match = regex.exec(str)
  return parseInt(match[1], 10)
}

export const sortSpriteFrame = (spriteFrames: SpriteFrame[]) => {
  return spriteFrames.sort((a: SpriteFrame, b: SpriteFrame) => {
    return getNumberInString(a.name) - getNumberInString(b.name)
  })
}

export const randomByLen = (len: number) => {
  return Array.from({ length: len }).reduce<string>((total, item) => total + Math.floor(Math.random() * 10), '')
}
